# encoding=utf-8

import time
import jieba
import flask
import jieba.posseg as pseg
from cls.orm.main import PG

app = flask.Flask(__name__)
app.config['JSON_AS_ASCII'] = False
db = PG()
dbcnn = db.cnn.cursor()
# app = flask.Flask(__name__, static_folder='', static_url_path='')
@app.route('/ls/<e>')
def testFun(e):
    words = pseg.cut(e)
    for word in words:
        updateWord(word)
    return '你输入的参数是：' + e

def updateWord(word):
    # res = db.exec("select id from words where word = '" + word[0] + "'")
    res = db.exec("select id from words where word = '" + word.word + "'")
    # utime = '2019-10-10 15:34:20'
    utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    if len(res) > 0:
        dbcnn.execute("update words set nums = nums + 1," + "updated_at = '" + utime + "'"
                      + " where word = '" + word.word + "'")
    else:
        dbcnn.execute("insert into words(word,nums,tag,updated_at) values ( %s, %s, %s ,%s)", (word.word, 1, word.flag, utime))
    db.cnn.commit()



if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)